package com.bwie.shop.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.shop.entity.ShopEntity;
import com.bwie.shop.service.ShopService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author ShuangzhiZhao
 * @email 466505908@qq.com
 * @date 2022-02-17 08:57:31
 */
@RestController
@RequestMapping("shop/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shopService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * es搜索列表
     */
    @RequestMapping("/eslist")
    public R esList(@RequestParam Map<String, Object> params) throws IOException {
        PageUtils page = shopService.esQueryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{sid}")
    public R info(@PathVariable("sid") Integer sid){
		ShopEntity shop = shopService.getById(sid);

        return R.ok().put("shop", shop);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ShopEntity shop){
		shopService.save(shop);

        return R.ok();
    }
    /**
     * 添加
     */
    @RequestMapping("/add")
    public R add(@RequestBody ShopEntity shop){
        rabbitTemplate.convertAndSend("month-exchange","add.shop", JSON.toJSONString(shop));
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/upd")
    public R upd(@RequestBody ShopEntity shop){
        rabbitTemplate.convertAndSend("month-exchange","update.shop", JSON.toJSONString(shop));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ShopEntity shop){
		shopService.updateById(shop);

        return R.ok();
    }
    /**
     * 启用
     */
    @RequestMapping("/qi")
    public R qiyong(@RequestBody ShopEntity shop){
//        shopService.qi(shop);
        rabbitTemplate.convertAndSend("month-exchange","qi.shop", JSON.toJSONString(shop));

        return R.ok();
    }
    /**
     * 禁用
     */
    @RequestMapping("/jin")
    public R jinyong(@RequestBody ShopEntity shop){

        rabbitTemplate.convertAndSend("month-exchange","jin.shop", JSON.toJSONString(shop));

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] sids){
		shopService.removeByIds(Arrays.asList(sids));

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/del")
    public R del(@RequestBody Integer[] sids){

        rabbitTemplate.convertAndSend("month-exchange","del.shop", sids);

        return R.ok();
    }

}
